package com.enseirb.bdxmap.converter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.enseirb.bdxmap.dao.UserMarkerDAO;
import com.enseirb.bdxmap.dao.dto.UserMarkerDTO;
import com.enseirb.bdxmap.service.model.UserMarkerBO;

public class UserMarkerConverter implements MarkerConverter {
	
	private UserMarkerDAO userMarkerDAO;
	
	public UserMarkerConverter(Context context) {
		this.userMarkerDAO = new UserMarkerDAO(context);
	}

	@Override
	public List<UserMarkerBO> getData() {
		List<UserMarkerDTO>	markersDTO = userMarkerDAO.getData();
		List<UserMarkerBO>  markersBO = new ArrayList<UserMarkerBO>();
		for(UserMarkerDTO markerDTO : markersDTO) {
			UserMarkerBO currentBO = userMarkerDTOToBO(markerDTO);
			markersBO.add(currentBO);
		}
		return markersBO;
	}
	
	public void addMarker(UserMarkerBO marker) {
		userMarkerDAO.createMarker(userMarkerBOToDTO(marker));
	}

	private UserMarkerBO userMarkerDTOToBO(UserMarkerDTO markerDTO) {
		UserMarkerBO bo = new UserMarkerBO();
		bo.id = markerDTO.id;
		bo.name = markerDTO.name;
		bo.latitude = Double.valueOf(markerDTO.latitude);
		bo.longitude = Double.valueOf(markerDTO.longitude);
		bo.description = markerDTO.description;
		return bo;
	}
	
	private UserMarkerDTO userMarkerBOToDTO(UserMarkerBO markerBO) {
		UserMarkerDTO dto = new UserMarkerDTO();
		dto.id = markerBO.id;
		dto.name = markerBO.name;
		dto.latitude = String.valueOf(markerBO.latitude);
		dto.longitude = String.valueOf(markerBO.longitude);
		dto.description = markerBO.description;
		return dto;
	}

}
